/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.dashboard.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.tianyun.cloud.dashboard.configuration.ConfigRequest;
import org.tianyun.cloud.dashboard.configuration.ConfigResponse;
import org.tianyun.cloud.web.controller.BaseController;

/**
 * SampleController
 *
 * @auther ebert_chan
 */
@RestController
@RequestMapping("/console")
public class SampleController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleController.class);

    @Autowired
    private ConfigResponse configResponse;

    private ConfigRequest config;

    @RequestMapping("/config-pull/{application}")
    @ResponseBody
    public ConfigRequest pull(@PathVariable(name = "application") String application) {
        if (config != null) {
            return config;
        }

        config = new ConfigRequest();
        config.setNamespaces(configResponse.getNamespaces());
        config.setApplication(application);
        config.setTimestamp(System.currentTimeMillis());
        return config;
    }

    @PostMapping("/config-push")
    @ResponseBody
    public String push(@RequestBody ConfigRequest config) {
        return "OK";
    }

    @PostMapping("/config-update")
    @ResponseBody
    public String update(@RequestBody ConfigRequest config) {
        this.config = config;

        return "OK";
    }

}
